<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

const animationDuration = 6000;

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '240px'
    },
    data:{
        type: Array ,
        default: []
    }
  },
  data() {
    return {
      chart: null,
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  watch:{
    data: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.data)
    },
    contains(arr, dst) {
        var i = arr.length;
        while ((i -= 1)) {
            if (arr[i] == dst) {
            return i;
            }
        }
        return false;
    },
    setOptions(chartData) {
        let list = chartData;
        let data = chartData.map(item=>item.name);
        let values =chartData.map(item=>item.value);
        let values2 =chartData.map(item=>(parseInt(item.value)/20)*100);
        var color = ['rgba(37,195,116', 'rgba(40,138,239', 'rgba(34,192,201', 'rgba(248,195,248', 'rgba(100,255,249']
        let lineY = [];
        for (var i = 0; i < data.length; i++) {
        var x = i
        if (x > color.length - 1) {
            x = color.length - 1
        }
        var data2 = {
            name: data[i],
            color: color[x] + ')',
            value: values[i],
            itemStyle: {
            normal: {
                show: true,
                color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [{
                offset: 0,
                color: color[x] + ', 1)'
                }, {
                offset: 1,
                color: color[x] + ', 1)'
                }], false),
                barBorderRadius: 2
            },
            emphasis: {
                shadowBlur: 15,
                shadowColor: 'rgba(0, 0, 0, 0.1)'
            }
            }
        }
        lineY.push(data2)
        }
      this.chart.setOption({
        
        xAxis: {
            max: 100,
                splitLine: {show: false},
                axisLabel: {show: false},
                axisTick: {show: false},
                axisLine: {show: false},
                splitArea : {show : false}//保留网格区域
        },
        grid: {
            containLabel: true,
            left: 20,
            top: 10,
            right: 90,
            bottom: 10,
        },
        yAxis: [{
            data: data,
            inverse: true,
            axisLine: {
                show: false
            },
            axisTick: {
                show: false
            },
            axisLabel: {
                margin: 10,
                textStyle: {
                    fontSize: 14,
                    color: '#b6e6ff'
                },
            },
                splitArea : {show : false}//保留网格区域
        }],
        series: [{ //内
                type: 'bar',
                barWidth: 20,
                legendHoverLink: false,
                symbolRepeat: true,
                silent: true,
                itemStyle: {
                     color: {
                        type: 'linear',
                        x: 0,
                        y: 0,
                        x2: 1,
                        y2: 0,
                    }
                },
                data: lineY,
                z: 1,
                animationEasing: 'elasticOut'
            },
            {
                type: 'pictorialBar',
                animationDuration: 0,
                symbolRepeat: 'fixed',
                symbolMargin: '20%',
                symbol: 'rect',
                symbolSize: [10, 20],
                symbolBoundingData: 100,
                itemStyle: {
                    normal: {
                        color: '#2B4978'
                    }
                },
                label: {
                    normal: {
                        show: true,
                        position: 'right',
                        offset: [0, 2],
                        textStyle: {
                            color: '#fff',
                            fontSize: 20,
                            textShadowColor:'#00f2ff',
                            textShadowBlur:30,
                            textShadowOffsetX:0,
                            textShadowOffsetY:0,
                        },
                    formatter: '{c}%'
                    },
                },
                data: values,
                z: 0,
                animationEasing: 'elasticOut'
            },
            { //分隔
                type: 'pictorialBar',
                itemStyle: {
                    color: '#000'
                },
                symbolRepeat: 'fixed',
                symbolMargin: 4,
                symbol: 'rect',
                symbolClip: true,
                symbolSize: [2, 20],
                symbolPosition: 'start',
                symbolOffset: [0, 0],
                symbolBoundingData: 2000,
                data: list,
                z: 2,
                animationEasing: 'elasticOut'
            }
        ]
      })
    }
  }
}
</script>
